package com.ml.mall.utils;

import com.ml.mall.bean.res.partner.vo.PartnerDayDetailVo;
import com.ml.mall.bean.res.supplier.vo.SupplierDayDetailVo;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/6/8 22:03
 */
public class DimDateTransformUtils {


    public static List<SupplierDayDetailVo> getCurDateMonthDataInit(String startTime, String endTime, List<SupplierDayDetailVo> listVO) {
        Calendar instance = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = new Date();
        try {
            parse = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        instance.setTime(parse);
        int d = DateUtils.getDateDiff(startTime, endTime);

        List<SupplierDayDetailVo> list = new ArrayList<>(d);
        for(int i = 0;i<=d;i++){
            BigDecimal a = BigDecimal.ZERO;
            int c = 0;
            String format = sdf.format(instance.getTime());
            SupplierDayDetailVo vo = new SupplierDayDetailVo();
            if(listVO!=null&&listVO.size()>0){
                Map<String, SupplierDayDetailVo> collect = listVO.stream().collect(Collectors.toMap(SupplierDayDetailVo::getDateStr, Function.identity()));
                if(collect.get(format)!=null){
                    SupplierDayDetailVo  v =  collect.get(format);
                    a = v.getTotalSales();
                    c = v.getTotalPurchaserOrder();
                }
            }
            vo.setDateStr(format);
            vo.setTotalSales(a);
            vo.setTotalPurchaserOrder(c);
            list.add(vo);
            instance.add(Calendar.DAY_OF_MONTH,1);
        }
        return list;
    }

    public static List<PartnerDayDetailVo> getCurDateMonthDataInit2(String startTime, String endTime, List<PartnerDayDetailVo> listVO) {
        Calendar instance = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = new Date();
        try {
            parse = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        instance.setTime(parse);
        int d = DateUtils.getDateDiff(startTime, endTime);

        List<PartnerDayDetailVo> list = new ArrayList<>(d);
        for(int i = 0;i<d;i++){
            BigDecimal a = BigDecimal.ZERO;
            int c = 0;
            String format = sdf.format(instance.getTime());
            PartnerDayDetailVo vo = new PartnerDayDetailVo();
            if(listVO!=null&&listVO.size()>0){
                Map<String, PartnerDayDetailVo> collect = listVO.stream().collect(Collectors.toMap(PartnerDayDetailVo::getDateStr, Function.identity()));
                if(collect.get(format)!=null){
                    PartnerDayDetailVo  v =  collect.get(format);
                    a = v.getTotalSales();
                    c = v.getTotalPO();
                }
            }
            vo.setDateStr(format);
            vo.setTotalSales(a);
            vo.setTotalPO(c);
            list.add(vo);
            instance.add(Calendar.DAY_OF_MONTH,1);
        }
        return list;
    }

}
